<?php

namespace app\api\controller;


use app\common\model\SafetyPlan;
use app\common\model\SafetyPlanLog;
use think\Db;
use think\Request;

/**
 * @title 计划/记录
 */
class Plan extends \app\common\controller\Api
{
    protected $noNeedLogin = ['plan_list'];
    protected $noNeedRight = '*';

    /**
     * @title 添加计划
     * @description
     * @param name:type type:string require:1  other: default: desc:类型1=培训计划,2=演练计划
     * @param name:plan_time type:string require:1  other: default: desc:培训时间
     * @param name:plan_project type:string require:1  other: default: desc:培训项目
     * @param name:plan_place type:string require:1  other: default: desc:培训地点
     * @param name:lecturer type:string require:1  other: default: desc:授课人
     * @param name:plan_target_ids type:string require:1  other: default: desc:培训对象
     * @param name:plan_target_content type:string require:1  other: default: desc:培训目的
     * @return res:结果
     * @author
     * @url /api/plan/add
     * @method POST
     */
    public function add_plan(Request $request){
        $res = SafetyPlan::create(array_merge(input(), ["user_id"=>$this->auth->id]));
        $this->success("添加计划成功");
    }


    /**
     * @title 获取计划列表
     * @description 分页20条
     * @param name:type type:string require:1  other: default: desc:类型1=培训计划,2=演练计划
     * @return log_id:为空值的时候还没有生成记录
     * @author
     * @url /api/plan/plan_list
     * @method POST
     */
    public function plan_list(Request $request){
        $type = $request->param("type");
        $plan = SafetyPlan::where("type", $type)
            ->orderRaw("
                log_id is not null, updatetime desc
            ")
            ->paginate(20);
        $this->success("", $plan);
    }

    /**
     * @title 生成记录
     * @description
     * @param name:plan_id type:string require:1  other: default: desc:计划id(防雷安全不需要传该值)
     * @param name:type type:string require:1  other: default: desc:类型1=安全生产,2=防雷安全,3=演练
     * @param name:plan_time type:string require:1  other: default: desc:培训时间
     * @param name:plan_project type:string require:1  other: default: desc:培训项目
     * @param name:plan_place type:string require:1  other: default: desc:培训地点
     * @param name:lecturer type:string require:1  other: default: desc:授课人
     * @param name:plan_number type:string require:1  other: default: desc:计划人数
     * @param name:actual_number type:string require:1  other: default: desc:实到人数
     * @param name:plan_target_ids type:string require:1  other: default: desc:培训对象
     * @param name:plan_target_content type:string require:1  other: default: desc:培训目的
     * @param name:final_feel type:string require:1  other: default: desc:总结心得
     * @param name:images type:string require:1  other: default: desc:相关图片
     * @return res:结果
     * @author
     * @url /api/plan/add_log
     * @method POST
     */
    public function add_log(Request $request){
        $plan_id = $request->param("plan_id");
        $type = $request->param("type");

        if ($type !=2){
            $plan = SafetyPlan::get($plan_id);
            if (empty($plan)) {
                $this->error("不存在该计划不能生成相关记录");
            }
            if ($plan->log_id > 0) {
                $this->error("已经生成过相关记录，不能重复生成");
            }
        }
        $result = false;
        Db::startTrans();
        try {
            $result = SafetyPlanLog::create(array_merge($request->param(), ["user_id"=>$this->auth->id]));
            if ($type != 2) {
                $plan->log_id = $result->id;
                $plan->save();
            }
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();
        }

        if (false === $result) {
            $this->error("生成记录失败");
        }
        $this->success("生成记录成功");
    }


    /**
     * @title 查看记录详情
     * @description
     * @param name:log_id type:string require:1  other: default: desc:记录id
     * @return res:结果
     * @author
     * @url /api/plan/log_desc
     * @method POST
     */
    public function log_desc(Request $request){
         $log_id = $request->param("log_id");
         $this->success("", SafetyPlanLog::get($log_id,['user']));
    }



}
























